package org.ruiyichen.guizhou.agr.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.google.gson.reflect.TypeToken;

import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.base.BaseActivity;
import org.ruiyichen.guizhou.agr.base.DataManger;
import org.ruiyichen.guizhou.agr.bean.Response;
import org.ruiyichen.guizhou.agr.config.Constant;
import org.ruiyichen.guizhou.agr.imp.IIrrigatedAreaFilter;
import org.ruiyichen.guizhou.agr.net.OkHttpManager;
import org.ruiyichen.guizhou.agr.ui.mineview.BToast;
import org.ruiyichen.guizhou.agr.utils.StrUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/9/8.
 */

public class ModifyPasswordActivity extends BaseActivity implements View.OnClickListener {

    EditText et_login_old_password,
            et_login_new_password,
            et_login_confirm_password;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.df_activity_modify_password);
        bOkHttpManager = new OkHttpManager(this, bHandler);
        initData();
    }

    @Override
    protected void initViews() {
        setTitle("修改密码").setRightImage(0).setRightText(null);
        et_login_old_password = (EditText) findViewById(R.id.et_login_old_password);
        et_login_new_password = (EditText) findViewById(R.id.et_login_new_password);
        et_login_confirm_password = (EditText) findViewById(R.id.et_login_confirm_password);
    }

    @Override
    protected void initEvents() {
        findViewById(R.id.tv_login_in).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_login_in:
                if (checkData()) {
                    //保存
                    submit();
                }
                break;
        }
    }

    private boolean checkData() {
        String newPwd = et_login_new_password.getText().toString();
        String confirmPwd = et_login_confirm_password.getText().toString();
        if (StrUtil.isEmpty(newPwd) || StrUtil.isEmpty(confirmPwd)) {
            BToast.getInstance().show("输入密码为空");
            return false;
        }
        if (newPwd.length() < 6 || newPwd.length() > 12 || confirmPwd.length() < 6 || confirmPwd.length() > 12) {
            BToast.getInstance().show("输入密码不合法");
            return false;
        }
        return true;
    }

    private void initData() {
        Map<String, String> body = new HashMap<>();
//            token	String	登录成功返回的token
        body.put("token", DataManger.getInstance().getUserInfo().token);
//            id	String	家庭成员id
        bOkHttpManager.httpRequest(Constant.RequestType.SEND_MESSAGE, Constant.Http.POST, body, false, new
                TypeToken<Response<? extends IIrrigatedAreaFilter, Void>>() {
                }.getType(), true, false);
    }


    /**
     * 提交
     */
    private void submit() {
        Map<String, String> body = new HashMap<>();
//        token	String	登录成功返回的token
        body.put("token", DataManger.getInstance().getUserInfo().token);

        bOkHttpManager.httpRequest(Constant.RequestType.SEND_MESSAGE, Constant.Http.POST, body, false, new
                TypeToken<Response<Void, Void>>() {
                }.getType(), true, false);
    }

    @Override
    protected void onPostHandle(int requestType, Object response, boolean error, int errorCode, Object obj1, Object obj2) {
        super.onPostHandle(requestType, response, error, errorCode, obj1, obj2);
        if (error) {
            BToast.getInstance().show(getResources().getString(R.string.abnormal));
        } else {
            if (response == null) {
                BToast.getInstance().show(getResources().getString(R.string.datafailure));
            } else {
                if (requestType == Constant.RequestType.SEND_MESSAGE) {
                    Response<Map<String, String>, Void> result = (Response<Map<String, String>, Void>) response;
                    if (result.getCode() == 0) {
                        onBackPressed();
                        BToast.getInstance().show(result.getMsg());
                    } else {
                        BToast.getInstance().show(result.getMsg());
                    }

                }
            }
        }
    }

}
